package com.findmeastore.bc.impl;

import java.sql.Timestamp;
import java.util.List;

import org.springframework.beans.BeanUtils;

import com.findmeastore.bc.ILocalityBc;
import com.findmeastore.bo.CityBo;
import com.findmeastore.bo.LocalityBo;
import com.findmeastore.bo.StateBo;
import com.findmeastore.criteria.LocalityCriteria;
import com.findmeastore.dao.ILocalityDAO;
import com.findmeastore.dto.LocalityDto;
import com.findmeastore.pagination.Page;
import com.findmeastore.pagination.PageCriteria;

public class LocalityBcImpl implements ILocalityBc{
	private ILocalityDAO localityDao;

	public Integer getTotalCityCount(){
		return localityDao.getTotalCityCount();
	}
	
	public Integer getTotalLocalityCount(){
		return localityDao.getTotalLocalityCount();
	}
	
	public List<StateBo> getAllStates(){
		return localityDao.getAllStates();
	}
	
	public List<CityBo> getAllCities(){
		return localityDao.getAllCities();
	}
	
	public List<LocalityBo> getAllLocalities(){
		return localityDao.getAllLocalities();
	}
	
	public LocalityBo getLocalityById(Integer id){
		return localityDao.getLocalityById(id);
	}
	
	public List<LocalityBo> getLocalitiesByCriteria(LocalityCriteria criteria){
		return localityDao.getLocalitiesByCriteria(criteria);
	}
	
	public Page getLocalitiesPageByCriteria(LocalityCriteria localityCriteria, PageCriteria pageCriteria){
		return localityDao.getLocalitiesPageByCriteria(localityCriteria, pageCriteria);
	}
	
	public void saveLocality(LocalityDto dto){
		Integer id = dto.getId();
		LocalityBo localityBo = null;
		if(id!=null&&id>0){//update
			localityBo = localityDao.getLocalityById(id);
		}else {//save
			localityBo = new LocalityBo();
			localityBo.setCreationTstamp(new Timestamp(System.currentTimeMillis()));
			localityBo.setCreatedByUserId(dto.getModifiedById());
		}
		BeanUtils.copyProperties(dto, localityBo);
		localityBo.setLastModifiedTstamp(new Timestamp(System.currentTimeMillis()));
		localityBo.setLastModifiedByUserId(dto.getModifiedById());

		localityDao.saveLocality(localityBo);
	}
	
	public ILocalityDAO getLocalityDao() {
		return localityDao;
	}

	public void setLocalityDao(ILocalityDAO localityDao) {
		this.localityDao = localityDao;
	}
}
